<?php

class controller_index extends model_index
{
    //获取参数
    private function desktop_get_args($get = true){
        $para = $get ? $_GET : $_POST;
        $args = array();
        $yestoday = strtotime(date('Y-m-d',time())) - 86400;
        $fromTime = date('Y-m-d',$yestoday);
        $args['sKey'] = $this->existsKeyInArray('sKey',$para,'S');
        $args['ps'] = $this->existsKeyInArray('ps',$para,'N',$this->pagesize);
        $args['fromTime'] = $this->existsKeyInArray('fromTime',$para,'S',$fromTime);
        $args['toTime'] = $this->existsKeyInArray('toTime',$para,'S',$fromTime);
        $args['liqtype'] = $this->existsKeyInArray('liqtype',$para,'N');
        $args['field'] = $this->existsKeyInArray('field',$para,'S');
        $args['wherefield'] = $this->existsKeyInArray('wherefield',$para,'S');
        return $args;
    }
    //拼装页面参数
    private function desktop_page_args($args)
    {
        $page_args = array();
        if(!empty($args['sKey'])) $page_args['sKey'] = $args['sKey'];
        if(!empty($args['ps'])) $page_args['ps'] = $args['ps'];
        if(!empty($args['fromTime'])) $page_args['fromTime'] = $args['fromTime'];
        if(!empty($args['toTime'])) $page_args['toTime'] = $args['toTime'];
        if(!empty($args['liqtype'])) $page_args['liqtype'] = $args['liqtype'];
        if(!empty($args['field'])) $page_args['field'] = $args['field'];
        if(!empty($args['wherefield'])) $page_args['wherefield'] = $args['wherefield'];
        return $page_args;
    }

//    public function desktop(){
//        $basepath = '/index/desktop';
//
//        $get = true;
//        $args = $this->desktop_get_args($get);
//        $page_args = $this->desktop_page_args($args);
//        $args['page_current'] = $this->get_page();
//        $datas = $this->mod_desktop($args);
//
//        $dateTitle = $args['fromTime'];
//
//        if($args['fromTime'] != $args['toTime']){
//            $dateTitle = '从 '.$args['fromTime'].' 至 '.$args['toTime'];
//        }
//
//        $this->display($basepath,array(
//            'basepath' => $basepath,
//            'datas' => $datas,
//            'thishref' => $basepath,
//            'dateTitle' => $dateTitle,
//            'page_url' => http_build_query($page_args),
//            'extra' => '',
//            'searchdata' => $args,
//            'container' => '#iframe'
//        ));
//    }

    public function desktop()
    {
        $basepath = '/index/desktop';
        $get = true;
        $args = $this->desktop_get_args($get);
        $page_args = $this->desktop_page_args($args);
        $args['page_current'] = $this->get_page();
        $datas = $this->mod_desktop($args);
        $dateTitle = $args['fromTime'];
        if($args['fromTime'] != $args['toTime']){
            $dateTitle = '从 '.$args['fromTime'].' 至 '.$args['toTime'];
        }
        $this->display($basepath,array(
            'basepath'  => $basepath,
            'datas'     => $datas,
            'thishref'  => $basepath,
            'dateTitle' => $dateTitle,
            'page_url'  => http_build_query($page_args),
            'extra'     => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }

    public function desktop_list()
    {
        $basepath = '/index/game_test';
        $this->display($basepath);
    }

    # region 二维数组根据$sortKey字段排序
    /**
     * 二维数组根据$sortKey字段排序
     * @param $arr 要排序的二维数组
     * @param $sortKey 排序字段
     * @param string $direction 排序方向（DESC 降序；ASC 升序）
     * @return array
     */
    public function sortArray($arr, $sortKey, $direction = "ASC")
    {
        $arrSort = array();
        foreach($arr AS $uniqid => $row){
            foreach($row AS $key=>$value){
                $arrSort[$key][$uniqid] = $value;
            }
        }
        array_multisort($arrSort[$sortKey], constant("SORT_".$direction), $arr);
        return $arr;
    }
    # endregion

    # region 在二维数组中查找指定$key并返回指定$resultKey的值
    /**
     * 在二维数组中查找指定$key并返回指定$resultKey的值
     * @param $arr  二维数组
     * @param $key  要查找的数组列
     * @param $value 查找的列的值
     * @param $resultKey 要返回数组中的列名
     * @return int
     */
    public function getOddsWinSort($arr, $key, $value, $resultKey)
    {
        $result = 0;
        foreach($arr as $keyp=>$valuep){
            if($valuep[$key]==$value){
                $result = $valuep[$resultKey];
            }
        }
        /*
        $result = array_filter($arr, function($var){
            return $var[$resultKey] == $value;
        });
        */

        return $result;
    }
    # endregion

    # region 随机抽取红包金额
    public function randArrayCoin($arr){
        $result = array(
            'coin' => 0,
            'redpackets' => array(),
            'key' =>0,
        );
        if(count($arr)>=1) {
            $key = array_rand($arr, 1);
            $value = $arr[$key]['coin'];
            $result = array(
                'coin' => $value,
                'redpackets' => $arr,
                'key' => $key,
            );
        }
        return $result;
    }
    # endregion

    public function redPacketRound($total,$num=5,$mantissa=0){
        //$total=10;//红包总额
        //$num=8;// 分成8个红包，支持8人随机领取
        $old_total = $total;
        mt_srand(time());
        $min = ($total / $num / 2) / 100;//每个人最少能收到的金额
        //if(intval($mantissa)>0) $minrand =  ($mantissa / 100);
        //if(intval($mantissa)==0) $minrand = mt_rand(1,9) / 100;
        //$min += $minrand;
        if($min < 0.01) $min = 0.01;
        $datas = array();
        for ($i=1;$i<$num;$i++)
        {
            $safe_eg = (float)($num-$i);
            // $safe_total=((float)$total - ($num-$i) * $min-$minrand) / (float)($safe_eg);//随机安全上限
            $safe_total=((float)$total - ($num-$i) * $min) / (float)($safe_eg);//随机安全上限
            $money = random_int($min*100,$safe_total*100)/100;
            $total=$total-$money;
            $datas[$i] = array(
                'coin' => $this->formatCoin($money, 2),
            );
        }
        $datas[$num] = array(
            'coin' => $this->formatCoin($total),
        );
        shuffle($datas);
        //$key = array_rand($datas);
        //var_dump($key);
        //print_r('第'.($i).'个红包：'.$total.' 元，余额：'.$total.' 元 '."\n");
        return $datas;
    }
}